package com.whfc.fse.service.impl;

import com.whfc.common.enums.BindFlag;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.ListData;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.util.DateUtil;
import com.whfc.fse.dao.FseLiftDataMapper;
import com.whfc.fse.dao.FseLiftMapper;
import com.whfc.fse.dto.FseLiftDataDTO;
import com.whfc.fse.dto.FseLiftDataLogDTO;
import com.whfc.fse.dto.OpenApiFseLiftDataDTO;
import com.whfc.fse.entity.FseLift;
import com.whfc.fse.enums.FseLiftSide;
import com.whfc.fse.service.FseLiftDataService;
import com.whfc.influx.dao.FseLiftDataLogDao;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 升降机数据
 *
 * @author GuoDong_Sun
 * @date 2020/4/10
 */
@DubboService(interfaceClass = FseLiftDataService.class, version = "1.0.0", timeout = 5000)
public class FseLiftDataServiceImpl implements FseLiftDataService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private FseLiftMapper fseLiftMapper;

    @Autowired
    private FseLiftDataMapper fseLiftDataMapper;

    @Autowired
    private FseLiftDataLogDao fseLiftDataLogDao;

    @Override
    public List<FseLiftDataDTO> latest(Integer liftId) {
        logger.info("获取升降机最新数据服务,liftId:{}", liftId);
        FseLift fseLift = fseLiftMapper.selectByPrimaryKey(liftId);
        if (fseLift == null) {
            throw new BizException(ResultEnum.PARAM_ERROR.getCode(), ResultEnum.PARAM_ERROR.getMessage());
        }
        List<FseLiftDataDTO> liftDataList = fseLiftDataMapper.selectByLiftId(liftId);
        boolean leftBindFlag = BindFlag.BIND.getValue().equals(fseLift.getLeftBindFlag());
        boolean rightBindFlag = BindFlag.BIND.getValue().equals(fseLift.getRightBindFlag());
        //查看绑定情况 当有左右轿厢出现未绑定情况就对数据进行过滤
        if (!leftBindFlag || !rightBindFlag) {
            List<FseLiftDataDTO> list = new ArrayList<>();
            for (FseLiftDataDTO fseLiftDataDTO : liftDataList) {
                if (FseLiftSide.LEFT.getValue().equals(fseLiftDataDTO.getLiftSide()) && leftBindFlag) {
                    list.add(fseLiftDataDTO);
                }
                if (FseLiftSide.RIGHT.getValue().equals(fseLiftDataDTO.getLiftSide()) && rightBindFlag) {
                    list.add(fseLiftDataDTO);
                }
            }
            return list;
        }
        return liftDataList;
    }

    @Override
    public OpenApiFseLiftDataDTO latest(String sn) throws BizException {
        logger.info("获取升降机实时数据,sn:{}", sn);
        FseLift fseLift = fseLiftMapper.selectByPlatformAndSn(null, sn);
        if (fseLift == null) {
            return null;
        }
        List<FseLiftDataDTO> fseLiftDataList = fseLiftDataMapper.selectByLiftId(fseLift.getId());
        for (FseLiftDataDTO fseLiftDataDTO : fseLiftDataList) {
            if (sn.equals(fseLiftDataDTO.getLeftSn()) || sn.equals(fseLiftDataDTO.getRightSn())) {
                OpenApiFseLiftDataDTO openApiFseLiftDataDTO = new OpenApiFseLiftDataDTO();
                BeanUtils.copyProperties(fseLiftDataDTO, openApiFseLiftDataDTO);
                return openApiFseLiftDataDTO;
            }
        }
        return null;
    }

    @Override
    public ListData<FseLiftDataLogDTO> log(Integer liftId, Date date) {
        logger.info("获取升降机监控的历史数据服务,liftId:{},date:{}", liftId, date);
        Date startTime = DateUtil.getDateBegin(date);
        Date endTime = DateUtil.getDateEnd(date);
        List<FseLiftDataLogDTO> list = fseLiftDataLogDao.selectByLiftIdAndTime(liftId, startTime, endTime);
        return new ListData<>(list);
    }


    @Override
    public ListData<FseLiftDataLogDTO> log(String sn, Date startTime, Date endTime) throws BizException {
        logger.info("获取升降机监控的历史数据服务,liftId:{},startTime:{},endTime：{}", sn, startTime, endTime);
        if ((double) (endTime.getTime() - startTime.getTime()) / 60000 > 30) {
            throw new BizException(ResultEnum.PARAM_ERROR.getCode(), "时间范围最大为30分钟");
        }
        FseLift fseLift = fseLiftMapper.selectByPlatformAndSn(null, sn);
        if (fseLift == null) {
            throw new BizException(ResultEnum.PARAM_ERROR.getCode(), "硬件不存在");
        }
        Integer leftSide;
        if (sn.equals(fseLift.getLeftSn())) {
            leftSide = FseLiftSide.LEFT.getValue();
        } else {
            leftSide = FseLiftSide.RIGHT.getValue();
        }
        List<FseLiftDataLogDTO> list = fseLiftDataLogDao.selectByLiftIdAndTime(fseLift.getId(), startTime, endTime, leftSide);
        return new ListData<>(list);
    }

}
